package com.wiz.web.controllor;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.annotation.Resource;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.reflect.TypeToken;
import com.wiz.web.service.PayService;
import com.wiz.web.vo.criteria.PayCriteria;

/**
 * 支付接口Controllor，主要适用于支付
 */
@Controller
@RequestMapping("/pay")
public class PayControllor extends BaseControllor {
	private transient final Logger log = LoggerFactory.getLogger(getClass());

	@Resource
	private PayService payService;

	/**
	 * 三方条码支付
	 */
	@RequestMapping(value = "/bar", method = RequestMethod.POST)
	@ResponseBody
	public String tripPayOrder(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			@RequestParam(value = "payChannel") int payChannel,
			HttpSession session, Model model){
		String order = null;
		try{
			order = getJsonStrByParam(stationId, params);
			return payService.barPay(order,hqId,stationId,payChannel);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("tripPayOrder error in "+stationId+"  params="+order+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 余额支付
	 */
	@RequestMapping(value = "/balance", method = RequestMethod.POST)
	@ResponseBody
	public String balancePay(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String order = null;
		try{
			order = getJsonStrByParam(stationId, params);
			log.info("someone pay in balance,params="+order);
			PayCriteria criteria = gson.fromJson(order, new TypeToken<PayCriteria>(){}.getType());
			return payService.balancePay(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("balancePay error in "+stationId+"  params="+order+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
}
